package 多线程;

public class useInterruptMeth {
    
    public static void main(String[] args) throws InterruptedException{
        // // 使用interrupt打断阻塞线程（ wait, sleep, join），会抛出InterruptedException异常
        // Thread t1 = new Thread(() -> {
        //     System.out.println("t1...running...");
        //     try {
        //         Thread.sleep(5000L);
        //     } catch (InterruptedException e) {
        //         e.printStackTrace();
        //     }
        // }, "t1");

        // t1.start();
        // Thread.sleep(500L);
        // t1.interrupt();
        // System.out.println(t1.isInterrupted());

        // 打断正常线程，根据当前线程的isInterrupted打断标记判断是否退出
        // 标记默认为false，在主线程中调用interrupt方法后变为true
        Thread t2 = new Thread(() -> {
            while(true){
                Thread current = Thread.currentThread();
                boolean interrupted = current.isInterrupted();
                if(interrupted){
                    System.out.println("interrupted status: " + interrupted);
                    break;
                }
            }
        }, "t2");

        t2.start();
        Thread.sleep(500L);
        t2.interrupt();
    }
}
